<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function ajax(url, data) {
        return new Promise(function (resolve, reject) {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", url, true)
            xhr.send();//这里写post方式参数
            xhr.onreadystatechange = function () {//同步不写这一步
                if (xhr.readyState == 4) {
                    if (xhr.status == 200) {
                        // xhr.responseText
                        resolve(xhr.response)
                    } else {
                        reject("兄嘚不太对啊")
                    }
                }
            }
        })
    }
    ajax("./data/1.json").then((d) => {
        console.log(d)
        return ajax("./data/2.json")
    })
        .then((d) => {
            console.log(d)
        })
        .catch(
            (s) => {
                console.log("error")
            }
        )


    //*----------------------------------------------------------------------------------------


    async function gitList() {
        try {//原生的捕获错误方法
            let x = await ajax("./data/1.json")//await 等待 
            //如果 promise结果时成功就走下一步，如果失败就死掉
            //不会再往下走
            let y = await ajax("./data/2.json")
            console.log(x, y)
        } catch {
            console.log("不太对")//都正确时，走不到这里
        }
    }

    gitList()


</script>

</html>